<template>
  <div class="app-container" style="overflow: scroll">
    <div style="width: 1024px;margin: 0 auto">
    <el-form ref="submitParams" :model="submitParams" :rules="rules" label-width="120px">
      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>代收付协议中心状态查询</span>
        </div>
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="查询方式" prop="queryType">
              <el-select
                v-model="submitParams.queryType"
                clearable
                placeholder="查询方式"
                style="width: 100%"
                @change="queryTypeChange"
              >
                <el-option
                  v-for="dict in dictMap.QUERY_STYLE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>协议信息</span>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="协议号" prop="proto">
              <el-input v-model="submitParams.proto" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="接收行" prop="rcvbrnoType">
              <el-select
                v-model="submitParams.rcvbrnoType"
                clearable
                placeholder="接收行"
                style="width: 100%"
                @change="rcvbrnoTypeChange"
              >
                <el-option
                  v-for="dict in dictMap.RCVBRNO_TYPE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="接收行行号" prop="rcvBrno">
              <el-input v-model="submitParams.rcvBrno" @change="checkbankcode('rcvBrno')" :disabled="rcvBrnoDisable" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width='20px'>
              <el-button
                icon="el-icon-search"
                plain
                type="primary"
                :style="{ display: visibleHiden }"
                @click="openBankBranchDialog('rcvBrno')"
              >
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="接收行行名" prop="rcvBrnoname">
              <el-input v-model="submitParams.rcvBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="付款人账号" prop="payerActno">
              <el-input v-model="submitParams.payerActno" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="付款人名称" prop="payerName">
              <el-input v-model="submitParams.payerName" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="付款人开户行行号" prop="payerAccBrno">
              <el-input v-model="submitParams.payerAccBrno" @change="checkbankcode('payerAccBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width='20px'>
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerAccBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="付款人开户行行名" prop="payerAccBrnoname">
              <el-input v-model="submitParams.payerAccBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="付款行行号" prop="payerBrno">
              <el-input v-model="submitParams.payerBrno" @change="checkbankcode('payerBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width='20px'>
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="付款行行名" prop="payerBrnoname">
              <el-input v-model="submitParams.payerBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="付款人清算行行号" prop="payerStBrno">
              <el-input v-model="submitParams.payerStBrno" @change="checkbankcode('payerStBrno')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width='20px'>
              <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('payerStBrno')">
                行名行号查询
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="付款人清算行行名" prop="payerStBrnoname">
              <el-input v-model="submitParams.payerStBrnoname" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="收款人名称" prop="payeeName">
              <el-input v-model="submitParams.payeeName" :disabled="payeeNameDisable" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <el-row>
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" @click="submitForm" v-loading.fullscreen.lock="loading">提 交</el-button>
            <el-button @click="refresh">重 置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
    </div>
    </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import BankBranchDialog from "@/views/components/BankBranchDialog";
import { getBankInfByBankCode } from "@/api/cnaps/common/bankInfo";
import { AgentPayInOutStatusQuerySave } from "@/api/cnaps/beps/392";
export default {
  name: "AgentPayInOutStatusQuery",
  mixins: [listPageMixin],
  components: { BankBranchDialog },
  data() {
    return {
      // 录入参数
      submitParams: {
        queryType: undefined,
        proto: undefined,
        protoType: undefined,
        rcvbrnoType: undefined,
        rcvBrno: undefined,
        rcvBrnoname: undefined,
        payerActno: undefined,
        payerName: undefined,
        payerAccBrno: undefined,
        payerAccBrnoname: undefined,
        payerBrno: undefined,
        payerBrnoname: undefined,
        payerStBrno: undefined,
        payerStBrnoname: undefined,
        payeeName: undefined,
      },

      loading: false,

      payeeNameDisable: false,

      rcvBrnoDisable: false,

      visibleHiden: undefined,
      //
      searchFormDictList: ["PROTOCOL_TYPE", "RCVBRNO_TYPE", "QUERY_STYLE"],

      rules: {
        queryType: [{ required: true, message: "请输入查询方式", trigger: "blur" }],
        proto: [{ required: true, message: "请输入协议号", trigger: "blur" }],
        rcvbrnoType: [{ required: true, message: "请输入接收行", trigger: "blur" }],
        rcvBrno: [
          { required: true, message: "请输入接收行行号", trigger: ['blur', 'change'] },
          { max: 14, message: "长度最大为14位", trigger: "blur" },
        ],
        payerActno: [
          { required: true, message: "请输入付款人账号", trigger: "blur" },
          { max: 32, message: "长度最大为32位", trigger: "blur" },
        ],
        payerName: [
          { required: true, message: "请输入付款人名称", trigger: "blur" },
          { max: 60, message: "长度最大为60位", trigger: "blur" },
        ],
        payerAccBrno: [{ max: 14, message: "长度最大为14位", trigger: "blur" }],
        payerBrno: [{ max: 14, message: "长度最大为14位", trigger: "blur" }],
        payerStBrno: [{ max: 14, message: "长度最大为14位", trigger: "blur" }],
        payeeName: [
          { required: true, message: "请输入收款人名称", trigger: "blur" },
          { max: 60, message: "长度最大为60位", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    //行名行号返回
    confirmBankBranch(fromFlag, row) {
      if (fromFlag === "payerAccBrno") {
        this.$set(this.submitParams, "payerAccBrno", row.bankCode);
        this.$set(this.submitParams, "payerAccBrnoname", row.orgFullname);
      } else if (fromFlag === "payerBrno") {
        this.$set(this.submitParams, "payerBrno", row.bankCode);
        this.$set(this.submitParams, "payerBrnoname", row.orgFullname);
      } else if (fromFlag === "payerStBrno") {
        this.$set(this.submitParams, "payerStBrno", row.bankCode);
        this.$set(this.submitParams, "payerStBrnoname", row.orgFullname);
      } else if (fromFlag === "rcvBrno") {
        this.$set(this.submitParams, "rcvBrno", row.bankCode);
        this.$set(this.submitParams, "rcvBrnoname", row.orgFullname);
      }
    },
    //行名行号查询
    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["submitParams"].validate((valid) => {
        if (valid) {
          this.loading = true;
          AgentPayInOutStatusQuerySave(this.submitParams)
            .then((response) => {
              this.msgSuccess("提交成功");
              this.refresh();
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
    //点击重置按钮
    refresh() {
      this.resetForm("submitParams");
      this.payeeNameDisable = false;
      this.rcvBrnoDisable = false;
    },

    checkbankcode(fromFlag) {
      if (fromFlag === "payerAccBrno") {
        let payerAccBrno = this.submitParams.payerAccBrno;
        getBankInfByBankCode(payerAccBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.submitParams, "payerAccBrno", response.result.bankCode);
              this.$set(this.submitParams, "payerAccBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.submitParams, "payerAccBrno", null);
              this.$set(this.submitParams, "payerAccBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "payerAccBrno", null);
            this.$set(this.submitParams, "payerAccBrnoname", null);
          });
      } else if (fromFlag === "payerBrno") {
        let payerBrno = this.submitParams.payerBrno;
        getBankInfByBankCode(payerBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.submitParams, "payerBrno", response.result.bankCode);
              this.$set(this.submitParams, "payerBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.submitParams, "payerBrno", null);
              this.$set(this.submitParams, "payerBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "payerBrno", null);
            this.$set(this.submitParams, "payerBrnoname", null);
          });
      } else if (fromFlag === "payerStBrno") {
        let payerStBrno = this.submitParams.payerStBrno;
        getBankInfByBankCode(payerStBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.submitParams, "payerStBrno", response.result.bankCode);
              this.$set(this.submitParams, "payerStBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.submitParams, "payerStBrno", null);
              this.$set(this.submitParams, "payerStBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "payerStBrno", null);
            this.$set(this.submitParams, "payerStBrnoname", null);
          });
      } else if (fromFlag === "rcvBrno") {
        let rcvBrno = this.submitParams.rcvBrno;
        getBankInfByBankCode(rcvBrno)
          .then((response) => {
            if (response.code == "S" && response.result !== null && response.result !== undefined) {
              this.$set(this.submitParams, "rcvBrno", response.result.bankCode);
              this.$set(this.submitParams, "rcvBrnoname", response.result.orgFullname);
            } else {
              this.msgError("无行名行号记录");
              this.$set(this.submitParams, "rcvBrno", null);
              this.$set(this.submitParams, "rcvBrnoname", null);
            }
          })
          .catch((err) => {
            this.$set(this.submitParams, "rcvBrno", null);
            this.$set(this.submitParams, "rcvBrnoname", null);
          });
      }
    },

    queryTypeChange() {
      console.log("queryTypeChange");
      let queryType = this.submitParams.queryType;
      if (queryType === "0") {
        this.$set(this.rules.proto[0], "required", true);
      } else {
        this.$set(this.rules.proto[0], "required", false);
      }
    },

    rcvbrnoTypeChange() {
      //todo 需要代付中心行号
      let rcvbrnoType = this.submitParams.rcvbrnoType;
      if (rcvbrnoType === "0") {
        this.$set(this.submitParams, "rcvBrno", "104100004370");
        this.checkbankcode("rcvBrno");
        this.visibleHiden = "none";
        this.rcvBrnoDisable = true;
      } else {
        this.$set(this.submitParams, "rcvBrno", undefined);
        this.$set(this.submitParams, "rcvBrnoname", undefined);
        this.visibleHiden = "";
        this.rcvBrnoDisable = false;
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>

